﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDService.SocketHandle
// 文 件 名：	PH06GetUpdateFiles
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2017/10/3 17:02:18
//========================================
using Common;
using Common.XmlHandle;
using Network.UpdateData;
using System;
using System.IO;
using System.Net.Sockets;

namespace SFJDService.SocketHandle
{
    class PH06GetUpdateFiles : PacketHandle
    {
        public PH06GetUpdateFiles(Socket socket, Packet packet) : base(socket, packet)
        {
        }

        protected override void Handle()
        {
            FileInfo fileInfo = null;
            Version version = new Version(Message);
            //bool hasUpdate = false;
            //bool hasSendFiles = false;
            try
            {
                fileInfo = new FileInfo(Runtime.thisUser.InPath + "Update\\SFJDService.exe");
            }
            catch { }
            Version fileVersion = null;
            if (fileInfo != null && fileInfo.Exists)
            {
                fileVersion = new Version(System.Diagnostics.FileVersionInfo.GetVersionInfo(Runtime.thisUser.InPath + "Update\\SFJDService.exe").ToString());
            }
            if(fileInfo == null)
            {
                this.CopyDirectory(Runtime.programPath, Runtime.thisUser.InPath + "Update\\");//将程序安装目录下的文件复制到接收目录下Update文件夹
                fileVersion = new Version(System.Diagnostics.FileVersionInfo.GetVersionInfo(Runtime.thisUser.InPath + "Update\\SFJDService.exe").ToString());
            }
            if (fileVersion > version)//如果接收文件夹文件版本大于请求的文件版本
            {
                ClassTransportInfo transInfo = new ClassTransportInfo(new string[] { Runtime.thisUser.InPath + "Update\\" });//创建文件传送信息

                //XmlTransport trans = new XmlTransport(TransportType.Upload, Path.GetTempPath());
                //trans.AddTransport(transInfo);//添加文件传送信息
                Runtime.xmlUpload.AddTransport(transInfo);
                                              //SendUdp(typeof(PH06GotUpdateFiles).GetDictianaryKey(), Runtime.thisUser.UserGuid, guidSend, "");
                Launcher.SendMsg2One(typeof(PH05Transport).GetDictianaryKey(), guidSend, transInfo.TransGuid.ToString());
                while (Runtime.onSending)
                {
                    Delay(200);
                }
                Launcher.SendMsg2One(typeof(PH06GotUpdateFiles).GetDictianaryKey(), guidSend, "");
            }
            if(fileVersion > Runtime.version)
            {
                Launcher.SendMsg2One(typeof(PH06GotUpdateFiles).GetDictianaryKey(), Runtime.thisUser.UserGuid, "");
            }
        }

        private void CopyDirectory(string srcPath, string destPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //获取目录下（不包含子目录）的文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)     //判断是否文件夹
                    {
                        if (!Directory.Exists(destPath + "\\" + i.Name))
                        {
                            Directory.CreateDirectory(destPath + "\\" + i.Name);   //目标目录下不存在此文件夹即创建子文件夹
                        }
                        CopyDirectory(i.FullName, destPath + "\\" + i.Name);    //递归调用复制子文件夹
                    }
                    else if (i.FullName.Substring(i.FullName.Length - 4, 4) != ".xml")
                    {
                        File.Copy(i.FullName, destPath + "\\" + i.Name, true);      //不是文件夹即复制文件，true表示可以覆盖同名文件
                    }
                }
            }
            catch
            {
                throw;
            }
        }
    }
}
